<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
        <el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="120px" label-position="left">
            <el-form-item label="问题" prop="question">
                <el-input v-model="form.question" clearable></el-input>
        	</el-form-item>
            <el-form-item label="答案" prop="answer">
                <el-input v-model="form.answer" clearable></el-input>
        	</el-form-item>
            <el-form-item label="A" prop="a">
                <el-input v-model="form.a" clearable></el-input>
        	</el-form-item>
            <el-form-item label="B" prop="b">
                <el-input v-model="form.b" clearable></el-input>
        	</el-form-item>
            <el-form-item label="C" prop="c">
                <el-input v-model="form.c" clearable></el-input>
        	</el-form-item>
            <el-form-item label="D" prop="d">
                <el-input v-model="form.d" clearable></el-input>
        	</el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="visible=false" >取 消</el-button>
            <el-button v-if="mode!='read'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
        </template>
    </el-dialog>
</template>

<script>
    
    export default {
        emits: ['success', 'closed'],
        
        data() {
            return {
                mode: "create",
                titleMap: {
                    read: '查看',
                    create: '新增',
                    update: '编辑',
                },
                visible: false,
                isSaveing: false,
                form: {
                    id: 0,
					question: '',
					answer: '',
					a: '',
					b: '',
					c: '',
					d: '',
                },
                rules: {
                },
            }
        },
        methods: {
            open(mode='create'){
                this.mode = mode;
                this.visible = true;
                return this
            },
            submit(){
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res = await this.$API.subj[this.mode].post(this.form);
                        this.isSaveing = false;
                        if(res.code == 200){
                            this.$emit('success', this.form, this.mode)
                            this.visible = false;
                            this.$message.success(res.message)
                        }else{
                            this.$alert(res.message, "提示", {type: 'error'})
                        }
                    }else{
                        return false;
                    }
                })
            },
            setData(data){
                this.$TABLE.assign(this,data);
            }
        }
    }
</script>